package service;

import helper.Credential;
import helper.PatientResult;
import helper.PredictionResult;

import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import manager.LoginManager;
import manager.PatientManager;
import manager.PredictionManager;
import manager.RuleManager;
import processor.RiskFactor;
import bean.PatientEnvironmentalExposure;
import bean.PatientPastHistory;
import bean.rule.RuleCondition;
import bean.rule.RuleParameter;

@WebService
public class HealthPredictionService {

	public HealthPredictionService() {

	}

	// ////////////////////////////////////////////Start - PatientManager
	// Methods////////////////////////////////////////////////
	
	/**
	 * 
	 */
	
	public Credential authenticateUser(String username, String password){
		return new LoginManager().authenticateUser(username, password);
	}
	
	/**
	 * 
	 * @param firstName
	 * @param middleName
	 * @param lastName
	 * @param sex
	 * @param ethnicity
	 * @param birthCountry
	 * @param ssn
	 * @param addressLine1
	 * @param addressLine2
	 * @param city
	 * @param state
	 * @param zip
	 * @param dateOfBirth
	 * @param homePhone
	 * @param workPhone
	 * @param email
	 * @param username
	 * @param password
	 * @return
	 */
	public String insertPatientGeneralInformation(String firstName,
			String middleName, String lastName, String sex, String ethnicity,
			String birthCountry, String ssn, String addressLine1,
			String addressLine2, String city, String state, long zip,
			Date dateOfBirth, String homePhone, String workPhone, String email,
			String username, String password) {

		String str = null;

		try {

			PatientManager patientManager = new PatientManager();

			str = patientManager.insertPatientGeneralInfo(firstName,
					middleName, lastName, sex, ethnicity, birthCountry, ssn,
					addressLine1, addressLine2, city, state, zip, dateOfBirth,
					homePhone, workPhone, email, username, password);

		} catch (Exception ex) {
			ex.printStackTrace();

		}

		return str;
	}

	public PatientResult[] listPatients() {

		PatientManager patientManager = new PatientManager();

		List<PatientResult> lstPatientGeneralInfo = patientManager
				.listPatients();

		PatientResult[] arrPatientGeneralInfo = new PatientResult[lstPatientGeneralInfo
				.size()];

		return lstPatientGeneralInfo.toArray(arrPatientGeneralInfo);

	}

	/**
	 * 
	 * @param patientId
	 * @return
	 */

	public PatientResult getPatientSummary(long patientId) {

		PatientManager patientManager = new PatientManager();

		return new PatientResult(patientManager.getPatientSummary(patientId));
	}

	/**
	 * 
	 * @return
	 */
	public RuleParameter[] getRuleParameters() {

		RuleManager ruleManager = new RuleManager();

		List<RuleParameter> lstRuleParameters = ruleManager.getRuleParameters();

		RuleParameter[] arrRuleParameters = new RuleParameter[lstRuleParameters
				.size()];

		return lstRuleParameters.toArray(arrRuleParameters);

	}

	/**
	 * 
	 * @param ruleName
	 * @param ruleDescription
	 * @param arrRuleConditions
	 * @param disease
	 * @return
	 */
	public String createRule(String ruleName, String ruleDescription,
			RuleCondition[] arrRuleConditions, String disease) {

		RuleManager ruleManager = new RuleManager();

		System.out.println("--------"+arrRuleConditions.length);
		
		String ruleId = ruleManager.createRule(ruleName, ruleDescription,
				arrRuleConditions, disease);

		return ruleId;
	}

	/**
	 * 
	 * @return
	 */
	public String[] getDiseases() {
		RuleManager ruleManager = new RuleManager();
		List<String> lstDiseases = ruleManager.getDiseases();
		String[] arrDiseases = new String[lstDiseases.size()];
		return lstDiseases.toArray(arrDiseases);
	}

	/**
	 * 
	 * @param patientId
	 * @param systolic
	 * @param diastolic
	 * @param testDate
	 * @return
	 */
	public String addBloodPressureReading(long patientId, double systolic,
			double diastolic, Date testDate) {
		return new PatientManager().addBloodPressure(patientId, systolic,
				diastolic, testDate);
	}

	/**
	 * 
	 * @param patientId
	 * @param fastingGlucose
	 * @param ogtt
	 * @param testDate
	 * @return
	 */
	public String addGlucoseTest(long patientId, double fastingGlucose,
			double ogtt, Date testDate) {
		return new PatientManager().addGlucoseTest(patientId, fastingGlucose,
				ogtt, testDate);
	}

	/**
	 * 
	 * @param asthma
	 * @param kidneyDisease
	 * @param depression
	 * @param diabetes
	 * @param eatingDisorder
	 * @param heartDisorder
	 * @param highBloodPressure
	 * @param highCholestrol
	 * @param thyroidDisease
	 * @param tuberculosis
	 * @param patientId
	 * @return
	 */
	public String addPatientPastMedicalHistory(boolean asthma,
			boolean kidneyDisease, boolean depression, boolean diabetes,
			boolean eatingDisorder, boolean heartDisorder,
			boolean highBloodPressure, boolean highCholestrol,
			boolean thyroidDisease, boolean tuberculosis, long patientId) {

		return new PatientManager().addPatientPastMedicalHistory(asthma,
				kidneyDisease, depression, diabetes, eatingDisorder,
				heartDisorder, highBloodPressure, highCholestrol,
				thyroidDisease, tuberculosis, patientId);
	}

	/**
	 * 
	 * @param asbestos
	 * @param lead
	 * @param diesel
	 * @param dust
	 * @param chemicals
	 * @param patientId
	 * @return
	 */
	public String addEnvironmentalExposure(boolean asbestos, boolean lead,
			boolean diesel, boolean dust, boolean chemicals, long patientId) {
		return new PatientManager().addEnvironmentalExposure(asbestos, lead,
				diesel, dust, chemicals, patientId);
	}
	
	/**
	 * 
	 * @param patientId
	 * @return
	 */
	public PatientPastHistory getPatientPastHistory(long patientId){
		return new PatientManager().getPatientPastHistory(patientId);		
	}
	
	/**
	 * 
	 * @param patientId
	 * @return
	 */
	public PatientEnvironmentalExposure getEnvironmentalExposure(long patientId){
		return new PatientManager().getEnvironmentalExposure(patientId);
	}
	
	/**
	 * 
	 * @param disease
	 * @param patientId
	 * @return
	 */
	public PredictionResult predictDisease(String disease, long patientId){
		PredictionManager manager = new PredictionManager();
		PredictionResult predictionResult = manager.predictDisease(disease, patientId);
		return predictionResult;
	}
	/**
	 * 
	 * @return
	 */
	public String[] getDiseasesWithRules(){
		RuleManager ruleManager = new RuleManager();
		List<String> lstDiseases = ruleManager.getDiseasesWithRules();
		String[] arrDiseases = new String[lstDiseases.size()];
		return lstDiseases.toArray(arrDiseases);
		
	}
	
}
